package com.acme.bankapp.client;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Scanner;

public class BankClientCLI {
	public static void main(String[] args) {
		if (args.length != 2) {
			System.out.println("Usage: BankClientCLI host port");
			return;
		}

		String host = args[0];
		int port = Integer.parseInt(args[1]);

		BankClient client = null;
		Scanner scanner = null;
		try {
			client = new BankClient(host, port);
			scanner = new Scanner(System.in);
			String message, response;

			do {
				System.out.print("client> ");
				message = scanner.nextLine();

				response = client.send(message);
				System.out.printf("server> %s%n%n", response);
			} while (!"bye".equals(response));

			scanner.close();

		} catch (UnknownHostException e) {
			System.out.println("unable to connect to the specified host");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {

			try {
				if (client != null)
					client.close();
				if (scanner != null)
					scanner.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}
}
